package com.chima.action;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Employees;
import com.chima.domain.Menu;
import com.chima.domain.Resource;
import com.chima.domain.Rose;
import com.chima.service.MenuService;
import com.chima.service.ResourceService;
import com.opensymphony.xwork2.ActionSupport;

public class TreeAction extends ActionSupport{
	private Integer id;
	private boolean success;
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	private  MenuService menuService;
	private List<Menu> menus;
	private List<Menu> menus1;
	private String  node;
	private ResourceService resourceService;
	
	
	public ResourceService getResourceService() {
		return resourceService;
	}
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	public String getNode() {
		return node;
	}
	public void setNode(String node) {
		this.node = node;
	}
	public MenuService getMenuService() {
		return menuService;
	}
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	private static final long serialVersionUID = 1L;
	  public String tree() throws Exception{
		  List names=new ArrayList();
		  List ids=new ArrayList();
		  HttpSession  session = ServletActionContext.getRequest().getSession();
		  Employees existEmployee=(Employees) session.getAttribute("existEmployee");
		  if(existEmployee!=null){
			  Rose rose=existEmployee.getRose();
			  List<Resource>  lResources=resourceService.resourcesfindbyroseid(rose.getId());
				 for(int j=0;j<lResources.size();j++){
					   names.add(lResources.get(j).getName());
					   ids.add(lResources.get(j).getId());
				   }
				   int id=Integer.parseInt(node);
					menus=menuService.getMenusbyid(id);
					if(menus.size()>0){
						String json="[";
						for(int i=0;i<menus.size();i++){
							if(names.contains(menus.get(i).getTitle())){
									if(!menus.get(i).isLeaf()){
										json+="{'id':'"+menus.get(i).getId()+"','text':'"+menus.get(i).getTitle()+"','leaf':'"+menus.get(i).isLeaf()+"','parentId':'"+menus.get(i).getParentid()+"','url':'"+menus.get(i).getUrl()+"'},";
									    menus1=menuService.getMenusbyid(menus.get(i).getId());
									}
									if(menus.get(i).isLeaf()){
										boolean ni=false;
										boolean hao=false;
										boolean ma=false;
										boolean search=false;
										List<Resource> resources=resourceService.resourcesfindreResources(menus.get(i).getId());
										if(resources.size()>0){
											for(int n=0;n<resources.size();n++){
													if(ids.contains(resources.get(n).getId())){
														
														if(resources.get(n).getName().equals("增加")){
															     ni=true;
												    	}
													   if(resources.get(n).getName().equals("修改")){
														    	hao=true;
												 		}
												    	if(resources.get(n).getName().equals("删除")){
																ma=true;
														}
												    	if(resources.get(n).getName().equals("查询")){
												    		search=true;
													}
													}
												}
											}
										json+="{'id':'"+menus.get(i).getId()+"','text':'"+menus.get(i).getTitle()+"','leaf':'"+menus.get(i).isLeaf()+"','parentId':'"+menus.get(i).getParentid()+"','url':'"+menus.get(i).getUrl()+"','add':'"+ni+"','edit':'"+hao+"','delete':'"+ma+"','search':'"+search+"'},";
										
									}
					    	}
						}
						json=json.substring(0,json.length()-1);
						json+="]";
						ServletActionContext.getResponse().setContentType("charset=utf-8");
						ServletActionContext.getResponse().getWriter().print(json);

					}else{
						String json="";
						ServletActionContext.getResponse().getWriter().print(json);	
					}  	
		  }
	   return NONE  ;
	}
	 public String powerandrose() throws Exception{
		 List<Resource>  lResources=resourceService.resourcesfindbyroseid(id);
		 if(lResources.size()>0){
		 this.setSuccess(true);
			JsonConfig jsonConfig = new JsonConfig();
			JSONArray jsonArray = JSONArray.fromObject(success,jsonConfig);
			JSONArray jsonArray1 = JSONArray.fromObject(lResources,jsonConfig);
			String jsonString = "{resouces:"+jsonArray1.toString()+"}";
		
		 
		System.out.println(jsonArray);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		 }
		 return NONE;
	 } 
  
}
